<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handleInputChange" />
    <a-button type="primary" @click="add">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox @click="changeDone(item.id)" :checked="item.done">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="moveItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="changeList(0)" :type="activeIndex==0?'primary':'default'">全部</a-button>
          <a-button @click="changeList(1)" :type="activeIndex==1?'primary':'default'">未完成</a-button>
          <a-button @click="changeList(2)" :type="activeIndex==2?'primary':'default'">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState,mapGetters } from "vuex";
export default {
  name: 'app',
  data() {
    return {
      
    }
  },
  created() {
    this.$store.dispatch('getList')
  },
  methods: {
    handleInputChange(e){
      // console.log(e.target.value)
      this.$store.commit('setInputValue',e.target.value)
    },

    // 向列表中添加数据
    add(){
      if(this.inputValue.trim().length<=0){
        return this.$message.warning('请输入内容')
      }
      this.$store.commit('addList')
    },

    // 删除数据
    moveItem(id){
      this.$store.commit('moveList',id)
    },

    // 改变复选框状态
    changeDone(id){
      this.$store.commit('changeDone',id)

    },

    // 清除已完成
    clearDone(){
      this.$store.commit('clearDone')
    },
    
    // 切换选项
    changeList(key){
      this.$store.commit('changeKey',key)
    }


  },
  computed: {
    ...mapState(['list','inputValue','activeIndex']),
    ...mapGetters(['unDoneLength','infoList'])
  },
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>